from module.const.Constant import const
from module.const import LocalStore

import logging
log = logging.getLogger("tasklog")

# 暂停策略任务流水线
def stopTaskThread(taskid):
    # [shortOptionDetail, 0, []]
    taskCenter = LocalStore.getTaskCenter(taskid)
    if not taskCenter:
        log.info("任务-%s 不存在，无法暂停" % taskid)
        return False
    elif taskCenter[1] == const.TASK_STATUS_STOP:
        log.info("任务-%s 已经手动暂停，无须再暂停" % taskid)
        return False
    elif taskCenter[1] == const.TASK_STATUS_DONE:
        log.info("任务-%s 已经执行成功完成，无须再暂停" % taskid)
        return False
    elif taskCenter[1] == const.TASK_STATUS_EXCEPT:
        log.info("任务-%s 执行异常失败了，无须再暂停" % taskid)
        return False

    taskCenter[1] = const.TASK_STATUS_STOP
    # [code, indexdate, starttime, endtime, 0]
    # for minitask in taskCenter[2]:
    #     if minitask[4] == const.TASK_STATUS_LOADING or minitask[4] == const.TASK_STATUS_RUNNING:
    #         minitask[4] = const.TASK_STATUS_STOP
    log.info("内部调工具类成功暂停了任务：%s" % taskCenter)

# 获取策略任务的 短线配置
def getShortOptionDetail(taskid):
    taskCenter = LocalStore.getTaskCenter(taskid)
    if not taskCenter:
        log.info("策略任务-%s 不存在，无法获取到短线配置详情" % taskid)
        return None
    return taskCenter[0]

# 拉取任务中心信息
def pullTaskCenter(taskid):
    return LocalStore.getTaskCenter(taskid)

# push任务中心信息
def pushTaskCenter(taskid, taskCenter):
    LocalStore.addTaskCenter(taskid, taskCenter)

# 获取任务调度管理
def getSuperTaskAdmin():
    return LocalStore.getTaskAdmin()

# 配置调度管理
def configTaskAdmin(taskAdmin):
    LocalStore.setTaskAdmin(taskAdmin)



